//
//  SupplierViewController.m
//  EastMarket
//
//  Created by 李鹏程 on 16/11/17.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "SupplierViewController.h"

@interface SupplierViewController ()

@end

@implementation SupplierViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"供应商";
    self.view.backgroundColor = EastMarket_backgroundColor;
    _dataSourceArr = [[NSMutableArray alloc] init];
    
    UIButton *btnAddSupplier = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [btnShare setBackgroundImage:[UIImage imageNamed:@"添加"] forState:UIControlStateNormal];
    [btnAddSupplier setTitle:@"新增" forState:UIControlStateNormal];
    [btnAddSupplier addTarget:self action:@selector(btnAddSupplier:) forControlEvents:UIControlEventTouchUpInside];
    [btnAddSupplier setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnAddSupplier.frame = CGRectMake(0, 0, 42, 18);
    UIBarButtonItem *btnAddSupplierItem = [[UIBarButtonItem alloc] initWithCustomView:btnAddSupplier];
    [self.navigationItem setRightBarButtonItem:btnAddSupplierItem];
    [self initSupplierUI];
    [self loadViewSellerList];
    [SVProgressHUD showWithStatus:@"加载中"];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
    [SVProgressHUD setForegroundColor:EastMarket_MainToneColor];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];

}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
}


- (void)initSupplierUI {

    _tabView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tabView.showsVerticalScrollIndicator = false;
    _tabView.delegate = self;
    _tabView.dataSource = self;
    //    _tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tabView.backgroundColor = EastMarket_backgroundColor;
    [self.view addSubview:_tabView];
    [self setExtraCellLineHidden:_tabView];
}

// 获取供应商
- (void)loadViewSellerList {
    
    [NSThread detachNewThreadSelector:@selector(callServerGetSellerList) toTarget:self  withObject:Nil];
}

- (void)callServerGetSellerList {
    
    NSDictionary *dicOfSellder = [AppServiceInterface GetSellerList];
    [self performSelectorOnMainThread:@selector(updateSellerList:) withObject:dicOfSellder waitUntilDone:NO];
}

- (void)updateSellerList:(NSDictionary *)dic {
    
    [SVProgressHUD dismiss];

    if (dic && [[dic objectForKey:@"success"] integerValue] == 1) {
        
        [_dataSourceArr addObjectsFromArray:[dic objectForKey:@"data"]];
        [_tabView reloadData];
    }
}

// 新增供应商
- (void)loadViewAddCustomer:(NSString *)invitationCode {

    _invitationCode = invitationCode;
   [NSThread detachNewThreadSelector:@selector(callServerAddCustomer) toTarget:self  withObject:Nil];
}

- (void)callServerAddCustomer {

    NSDictionary *resultDicOfAdd = [AppServiceInterface AddCustomer:_invitationCode];
    [self performSelectorOnMainThread:@selector(updateUIAfterAddCustomer:) withObject:resultDicOfAdd waitUntilDone:NO];
}

- (void)updateUIAfterAddCustomer:(NSDictionary *)dic {

    debugMethod();
    if (dic && [[dic objectForKey:@"success"] integerValue] == 1) {
        [GlobalUnit showAlertHint:@"" message:[NSString stringWithFormat:@"%@", [dic objectForKey:@"msg"]]];
    } else if (dic && [[dic objectForKey:@"success"] integerValue] == 0) {
    
        [GlobalUnit showAlertHint:@"温馨提示！" message:[NSString stringWithFormat:@"%@", [dic objectForKey:@"msg"]]];
    } else {
    
        [GlobalUnit showAlertHint:@"温馨提示！" message:@"请求服务器失败，请稍后重试"];
    }
}

- (void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}

#pragma mark -- UITableViewDelegate &  UITableViewDatasource

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cell";
    SupplierViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[SupplierViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    NSInteger curRow = [indexPath row];
    NSDictionary *sellerListDic = [_dataSourceArr objectAtIndex:curRow];
    
    cell.imgProductIcon.image = [UIImage imageNamed:@"NoProductImage"];
    cell.lblSuppierName.text = [NSString stringWithFormat:@"%@", [sellerListDic objectForKey:@"name"]];
    cell.lblSuppierPhone.text = [NSString stringWithFormat:@"电话: %@", [sellerListDic objectForKey:@"phone"]];
    cell.lblSupplierAddress.text = [NSString stringWithFormat:@"地址: %@", [sellerListDic objectForKey:@"address"]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger curRow = [indexPath row];
    NSDictionary *sellerListDic = [_dataSourceArr objectAtIndex:curRow];
    SupplierInfoViewController *supplierInfoVC = [[SupplierInfoViewController alloc] init];
    [supplierInfoVC setHidesBottomBarWhenPushed:false];
    supplierInfoVC.sellerId = [NSString stringWithFormat:@"%@", [sellerListDic objectForKey:@"id"]];
    supplierInfoVC.sellerName = [NSString stringWithFormat:@"%@", [sellerListDic objectForKey:@"name"]];
    
    globalManager.isAllProduct = true;
    [self.navigationController pushViewController:supplierInfoVC animated:true];
}

- (void)btnAddSupplier:(UIButton *)btn {

    debugMethod();
    UIAlertController *alerController = [UIAlertController alertControllerWithTitle:@"请输入供应商的邀请码" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    // 创建文本框
    [alerController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"邀请码";
        textField.secureTextEntry = NO;
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        debugLog(@"点击了取消按钮");
    }];
    
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        debugLog(@"点击了好的按钮");
        // 读取文本框的值显示出来
        UITextField *userEmail = alerController.textFields.firstObject;
        _txtFieldContent = [NSString stringWithFormat:@"%@", userEmail.text];
        [self loadViewAddCustomer:_txtFieldContent];
        debugLog(@"_txtFieldContent:    %@", _txtFieldContent);
    }];
    [alerController addAction:cancleAction];
    [alerController addAction:defaultAction];
    [self  presentViewController:alerController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
